package com.power.project.controller;

import com.power.common.domain.ResponseDTO;
import com.power.common.exception.CustomBizException;
import com.power.common.file.FileManager;
import com.power.common.file.model.FileMeta;
import com.power.common.util.DateUtil;
import com.power.project.model.HaAttachment;
import com.power.project.model.vo.FileVO;
import com.power.project.service.HaAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 文件服务
 */
@RestController
@Api(value = "文件服务", tags = {"文件服务"})
public class FileController {

    @Resource
    private FileManager fileService;

    @Resource
    private HaAttachmentService attachmentService;


    @ApiOperation(value = "文件Minio上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件流对象", required = true, dataType = "__File", paramType = "form", allowMultiple = true),
            @ApiImplicitParam(name = "bucket", value = "桶", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "path", value = "存放目录", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "tenantId", value = "租户id", required = true, paramType = "query", dataType = "string")
    })
    @PostMapping("/file/upload")
    public ResponseDTO<FileVO> minioUpload(@RequestPart("file") MultipartFile file,
                                           @RequestParam(name = "bucket", required = false) String bucket,
                                           @RequestParam(name = "path", required = false) String path,
                                           @RequestParam(name = "tenantId", required = false) String tenantId) throws Exception {
        if (StringUtils.isEmpty(path)) {
            path = DateUtil.d2s_simple(new Date());
        }

        FileMeta fileMeta = fileService.upload(file, bucket, path);
        if (fileMeta == null) {
            throw new CustomBizException("附件上传失败");
        }
        FileVO vo = new FileVO(fileMeta);
        HaAttachment attachment = new HaAttachment();
        attachment.setFilename(fileMeta.getFilename());
        attachment.setFiletype(fileMeta.getFiletype());
        attachment.setFilesize(fileMeta.getSize());
        attachment.setPath(fileMeta.getPath());
        attachment.setTenantid(tenantId);
        attachmentService.save(attachment);
        vo.setAttachmentId(attachment.getId());
        return ResponseDTO.succData(vo);
    }

    @ApiOperation(value = "获取文件访问地址")
    @GetMapping("/file/getUrl")
    public ResponseDTO<String> getUrl(@RequestParam("attachmentId") Integer attachmentId) {
        HaAttachment attachment = attachmentService.getById(attachmentId);
        if (attachment == null) {
            throw new CustomBizException("附件不存在");
        }
        return ResponseDTO.succData(fileService.getFileUrl(null, attachment.getPath()));
    }

    @ApiOperation(value = "获取附件信息")
    @GetMapping("/file/get")
    public ResponseDTO<FileVO> get(@RequestParam("attachmentId") Integer attachmentId) {
        HaAttachment attachment = attachmentService.getById(attachmentId);

        if (attachment == null) {
            throw new CustomBizException("附件不存在");
        }

        FileVO fileVO = new FileVO(attachment.getId(), attachment.getFilename(), fileService.getFileUrl(null, attachment.getPath()));

        return ResponseDTO.succData(fileVO);
    }

    @ApiOperation(value = "文件下载")
    @GetMapping("/file/download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attachmentId", value = "附件id", required = true, paramType = "query", dataType = "integer")
    })
    public ResponseEntity<byte[]> download(@RequestParam("attachmentId") Integer attachmentId, HttpServletRequest request) {
        HaAttachment attachment = attachmentService.getById(attachmentId);

        if (attachment == null) {
            throw new CustomBizException("附件不存在");
        }

        return fileService.download(null, attachment.getPath(), attachment.getFilename(), attachment.getFiletype(), request);

    }

}
